package com.guigu.tms.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.tms.base.mapper.PdTransportTripsMapper;
import com.guigu.tms.base.mapper.PdTruckLicenseMapper;
import com.guigu.tms.base.mapper.PdTruckMapper;
import com.guigu.tms.base.service.IPdTruckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.tms.commons.entity.base.PdTransportTrips;
import com.guigu.tms.commons.entity.base.PdTruck;
import com.guigu.tms.commons.entity.base.PdTruckLicense;
import com.guigu.tms.commons.entity.base.VehicleManagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author wj
 * @since 2022-09-26
 */
@Service
public class PdTruckServiceImpl extends ServiceImpl<PdTruckMapper, PdTruck> implements IPdTruckService {

    //车辆信息表
    @Autowired
    PdTruckMapper pdTruckMapper;

    //车次表
    @Autowired
    PdTransportTripsMapper pdTransportTripsMapper;

    //行驶证信息表
    @Autowired
    PdTruckLicenseMapper pdTruckLicenseMapper;

    @Override
    public PageInfo<VehicleManagement> findTruckList(Map<String, String> map) {
        String pageindex=map.get("pageindex");
        String pagenum=map.get("pagenum");
        int page=Integer.valueOf(pageindex)<=0?1:Integer.valueOf(pageindex);
        PageHelper.startPage(page,Integer.valueOf(pagenum));
        List<VehicleManagement> pdTrucks = pdTruckMapper.findTruckList(map);
        PageInfo<VehicleManagement> pageInfo = new PageInfo<>(pdTrucks);
        return pageInfo;
    }

    @Override
    public List<PdTruck> findAllPdTruck() {
        return pdTruckMapper.selectList(null);
    }

    @Override
    public int AddPdTruck(VehicleManagement management) {
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (999 - 100 + 1)) + 100;
        String s=str+rannum;
        management.setId(s);
        int insert = pdTruckMapper.insert(management);
        Integer truckId = management.getTruckId();
        PdTruckLicense pdTruckLicense=new PdTruckLicense();
        pdTruckLicense.setTruckId(truckId);
        pdTruckLicense.setId(s);
        pdTruckLicenseMapper.insert(pdTruckLicense);
        PdTransportTrips pdTransportTrips=new PdTransportTrips();
        pdTransportTrips.setTruckId(truckId);
        pdTransportTrips.setId(s);
        pdTransportTripsMapper.insert(pdTransportTrips);
        return insert;
    }

    @Override
    public int DeleteTruckByid(Integer truckId) {
        return pdTruckMapper.deleteById(truckId);
    }

    @Override
    public int SaveTruck(VehicleManagement vehicleManagement) {
        return pdTruckMapper.updateById(vehicleManagement);
    }
}
